/*
 * BanBu_SettingViewController
 * @description 本文件提供系统设置的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供系统设置的ViewController
 */


#import "BanBu_SettingViewController.h"
#import "BanBu_NavigationController.h"
#import "BanBu_StealthViewController.h"
#import "BanBu_LocationHelperController.h"
#import "BanBu_RemindViewController.h"
#import "BanBu_MuteViewController.h"
#import "BanBu_ChangePWViewController.h"
#import "BanBu_BlacklistViewController.h"
#import "BanBu_AppDelegate.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "BanBu_UserAgreement.h"
#import "BanBu_HelpViewController.h"
#import "BanBu_FeedbackView.h"
#import "BanBu_ListViewController.h"
#import "BanBu_BroadcastTVC.h"
#import "BanBu_SoundSetting.h"
#import "BanBu_ThemeViewController.h"
#import "BanBu_NavButton.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_ThemeAndBubbleViewController.h"
#import "BanBu_NewVersionViewController.h"
#import "UIBadgeView.h"
#import "SDImageCache.h"
#import "UIViewController+Animation.h"
#import "BanBu_WebFrame_ViewController.h"
#import "BanBu_DestoryAcountViewController.h"
#import "BanBu_MWNavigationController.h"
#import "BanBu_NoDisturbingViewController.h"
#import "BanBu_MainCell.h"
@interface BanBu_SettingViewController ()

@end

@implementation BanBu_SettingViewController

- (id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)dealloc
{
    [badgeView release],badgeView = nil;
    [super dealloc];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [AppComManager cancalHandlesForObject:self];
    NSLog(@"记录4:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
}
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    [UIView setAnimationsEnabled:YES];
//    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
    NSLog(@"记录3:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
}
-(void)popself
{
    NSLog(@"001:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:NO afterShow:0];
    [self.navigationController popViewControllerAnimated:YES];
//    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"记录viewDidLoad1:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    badgeView = [[UIBadgeView alloc] init];
    
//    BanBu_NavButton *btn_return=[[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeBack];
//    btn_return.position = ButtonPositionLeft;
//    [btn_return addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
//    CGFloat btnLen1 = [NSLocalizedString(@"returnButton", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 30)].width;
//    btn_return.frame=CGRectMake(0, 0, btnLen1+20, 30);
//
//    [btn_return setTitle:NSLocalizedString(@"returnButton", nil) forState:UIControlStateNormal];
//    btn_return.titleLabel.font = [UIFont systemFontOfSize:14];
//    UIBarButtonItem *bar_itemreturn=[[[UIBarButtonItem alloc] initWithCustomView:btn_return] autorelease];
//    self.navigationItem.leftBarButtonItem = bar_itemreturn;
//    [btn_return release];

    BanBu_ImageView *img = [[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable];
    self.tableView.backgroundView = img;
    [img release];
    

    self.tableView.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    self.title = NSLocalizedString(@"settingTitle", nil);
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
    footerView.backgroundColor = [UIColor clearColor];
    UIButton *exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    exitBtn.frame = CGRectMake(10, 10, 300,45);
    [exitBtn setBackgroundImage:[[UIImage imageNamed:@"btn_red.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [exitBtn setTitle:NSLocalizedString(@"exitBtn", nil) forState:UIControlStateNormal];
    [exitBtn addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:exitBtn];
    self.tableView.tableFooterView = footerView;
    [footerView release];
    NSLog(@"记录viewDidLoad2:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
}

- (void)viewDidUnload
{
    NSLog(@"002:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NSLog(@"003:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"004:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"005:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    if(section == 0)
    {
        return 5;
    }
    if(section == 2)//系统通知设置
    {
        return 3;
    }
    else if (section == 3)//邀请码
    {
        return 2;
    }
    if(section == 4)//获取邀请码
        return [[UserDefaults valueForKey:@"hidecell"] integerValue]+1;
//    if(section == 5)//邀请注册
//        return 1;
    if(section == 5)//注销账号
        return 1;

    return 6;//消息管理

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSLog(@"006:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    if(section == 0)
        return NSLocalizedString(@"userNameMan", nil);
    else if(section == 1)
        return NSLocalizedString(@"messageMan", nil);
    else if (section == 2)
        return NSLocalizedString(@"systemMessageSwitch", nil);
    return nil;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"007:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    static NSString *CellIdentifier = @"Cell";
    BanBu_MainCell *cell = (BanBu_MainCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[[BanBu_MainCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.backgroundView = nil;

        cell.textLabel.backgroundColor = [UIColor clearColor];
    }
    for (UIView * views in cell.contentView.subviews)
    {
        [views removeFromSuperview];
    }
    cell.detailTextLabel.text = nil;
    //使用全局的getThemeImage来切换cell的背景图片，保证背景图片能够及时的改变，同时重写viewwillappear方法来保证每次的cell都要读取一下主题背景图片  a little keng
    if([ThemeManager getThemeImage:@"tablecell_backimage.png"])
        cell.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:@"tablecell_backimage.png"]];
    else
        cell.backgroundColor = nil;

    if(indexPath.section == 0)//账号管理
    {
        cell.accessoryView = nil;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        if(!indexPath.row)
        {
            cell.textLabel.text = NSLocalizedString(@"PWSettingLabel", nil);
        }
        else if(indexPath.row==1)
        {
            cell.textLabel.text = NSLocalizedString(@"blackTitle", nil);
            
        }
        else if(indexPath.row ==2)
        {
            cell.textLabel.text = NSLocalizedString(@"interestingTheme", nil);
        }
        else if(indexPath.row == 3)
        {
            cell.textLabel.text = NSLocalizedString(@"stealth", nil);
        }
        else if (indexPath.row == 4)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            UISwitch *aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(310-80, 7, 80, 30)];
            [aSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            [aSwitch setAccessibilityLanguage:@"English"];
            cell.accessoryView = aSwitch;
            [aSwitch release];
            
            cell.textLabel.text=NSLocalizedString(@"setFollowsHidden", nil);
            _setFollowsHiddenSwitch = aSwitch;
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"setFollowsHidden"]boolValue])
            {
                _setFollowsHiddenSwitch.on = YES;
            }
            else
            {
                _setFollowsHiddenSwitch.on = NO;
            }
        }

        
    }
    else if(indexPath.section == 1)//消息设置
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(indexPath.row<=2)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            UISwitch *aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(310-80, 7, 80, 30)];
            [aSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            [aSwitch setAccessibilityLanguage:@"English"];
            cell.accessoryView = aSwitch;
            [aSwitch release];
            
            if(indexPath.row == 0)
            {
                cell.textLabel.text=NSLocalizedString(@"notificChat", nil);
                _messageSwitch = aSwitch;
                _messageSwitch.on = YES;
                _messageSwitch.userInteractionEnabled = NO;
            }
            else if(indexPath.row == 1)
            {
                cell.textLabel.text=NSLocalizedString(@"notificFriend", nil);
                _friendDosSwitch = aSwitch;
                NSLog(@"记录1:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
                
                if([[[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"boolKey"] objectAtIndex:1]boolValue])
                {
                    _friendDosSwitch.on = YES;
                }else{
                    _friendDosSwitch.on = NO;
                }
                NSLog(@"接收好友圈:%@",[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"boolKey"]);
            }
            else if(indexPath.row == 2)//发送已读状态开关
            {
                cell.textLabel.text=NSLocalizedString(@"systemset_friendreadstatus", nil);
                _sendReadStateSwitch = aSwitch;
                if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"systemset_friendreadstatus"]boolValue])
                {
                    _sendReadStateSwitch.on = YES;
                }
                else
                {
                    _sendReadStateSwitch.on = NO;
                }
            }

        }
        else if(indexPath.row == 3)//发消息提示音
        {
            UILabel * isOnLa = [[UILabel alloc]initWithFrame:CGRectMake(150, 10, 135, 22)];
            isOnLa.tag = 1001;
            isOnLa.textAlignment = kTextAlignmentRight;
            isOnLa.font = [UIFont systemFontOfSize:16.0];
            isOnLa.backgroundColor = [UIColor clearColor];
            isOnLa.textColor = [UIColor lightGrayColor];
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"sendMsgBoolKey"]length])
            {
                isOnLa.text = NSLocalizedString(@"open", nil);
            }
            else
            {
                isOnLa.text = NSLocalizedString(@"close", nil);
            }
            [cell.contentView addSubview:isOnLa];
            [isOnLa release];
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = NSLocalizedString(@"sendMsgBoolKey", nil);
        }
        else if(indexPath.row == 4)//接消息提示音
        {
            UILabel * isOnLa = [[UILabel alloc]initWithFrame:CGRectMake(150, 10, 135, 22)];
            isOnLa.tag = 1001;
            isOnLa.textAlignment = kTextAlignmentRight;
            isOnLa.font = [UIFont systemFontOfSize:16.0];
            isOnLa.backgroundColor = [UIColor clearColor];
            isOnLa.textColor = [UIColor lightGrayColor];

            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"MusicSwith"]length])
            {
                isOnLa.text = NSLocalizedString(@"open", nil);
            }
            else
            {
                isOnLa.text = NSLocalizedString(@"close", nil);
            }
            [cell.contentView addSubview:isOnLa];
            [isOnLa release];

            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = NSLocalizedString(@"soundSwitch", nil);
        }
        else if (indexPath.row == 5)//勿扰时段
        {
            UILabel * isOnLa = [[UILabel alloc]initWithFrame:CGRectMake(150, 10, 135, 22)];
            isOnLa.tag = 1001;
            isOnLa.textAlignment = kTextAlignmentRight;
            isOnLa.font = [UIFont systemFontOfSize:16.0];
            isOnLa.backgroundColor = [UIColor clearColor];
            isOnLa.textColor = [UIColor lightGrayColor];
            
            NSDictionary * settingsDic = [[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"];
            NSArray * timeArray = [settingsDic valueForKey:@"NoDisturbingTime"];
            if([[timeArray objectAtIndex:0] boolValue])
            {
                isOnLa.text = NSLocalizedString(@"open", nil);
            }
            else
            {
                isOnLa.text = NSLocalizedString(@"close", nil);
            }
            [cell.contentView addSubview:isOnLa];
            [isOnLa release];

            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = NSLocalizedString(@"sysPushAlertCloseSetup", nil);
            cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
        }
    }
    else if (indexPath.section == 2)//系统通知设置
    {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;// UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        UISwitch *aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(310-80, 7, 80, 30)];
        [aSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [aSwitch setAccessibilityLanguage:@"English"];
        cell.accessoryView = aSwitch;
        [aSwitch release];
        
        if(indexPath.row == 0)//我关注了你
        {
            cell.textLabel.text=NSLocalizedString(@"followYouNotification", nil);
            _followYouNotificationSwitch = aSwitch;
            
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"followYouNotification"] boolValue])
            {
                _followYouNotificationSwitch.on = YES;
            }
            else
            {
                _followYouNotificationSwitch.on = NO;
            }
        }
        else if(indexPath.row == 1)//我查看了你的个人资料
        {
            cell.textLabel.text=NSLocalizedString(@"visitYouNotification", nil);
            _visitYouNotificationSwitch = aSwitch;
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"visitYouNotification"] boolValue])
            {
                _visitYouNotificationSwitch.on = YES;
            }
            else
            {
                _visitYouNotificationSwitch.on = NO;
            }
        }
        else if(indexPath.row == 2)//新用户注册通知
        {
            cell.textLabel.text=NSLocalizedString(@"takeCareNotification", nil);
            _takeCareNotificationSwitch = aSwitch;
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"takeCareNotification"]boolValue])
            {
                _takeCareNotificationSwitch.on = YES;
            }
            else
            {
                _takeCareNotificationSwitch.on = NO;
            }
        }
    }
    else if(indexPath.section == 3)//其他
    {
        cell.accessoryView = nil;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        if(!indexPath.row)
        {
            cell.textLabel.text = NSLocalizedString(@"HalfareaTestingPlatform", nil);
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = NSLocalizedString(@"clearCache", nil);
        }
    }
    else if(indexPath.section == 4)
    {
        if(indexPath.row == 1)
        {
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
            cell.textLabel.text = NSLocalizedString(@"inviteFriendReg", nil);
        }
        else
        {
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
            cell.textLabel.text = NSLocalizedString(@"getInvationNum", nil);
        }

    }
    else if(indexPath.section == 5)//邀请注册
    {

        cell.accessoryView = nil;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.textLabel.text = NSLocalizedString(@"sysDestroyAccountTitle", nil);
    }
//    else if(indexPath.section == 6)//注销账号
//    {
//
//    }
    return cell;
}

#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"008:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    [self tableView:tableView didSelectRowAtIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"009:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0)
    {
        //密码设置
        if(indexPath.row==0)
        {
            BanBu_ChangePWViewController *changePW = [[BanBu_ChangePWViewController alloc] init];
//            BanBu_NavigationController *nav = [[[BanBu_NavigationController alloc] initWithRootViewController:changePW] autorelease];
//            if ([nav.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
//            {
//                UIImage *image = [ThemeManager getThemeImage:@"topbar.png"];
//                [nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//            }
//            [self presentViewController:nav animated:YES];
            [self.navigationController pushViewController:changePW animated:YES];
            [changePW release];
        }
        //黑名单
        else if(indexPath.row==1)
        {
            BanBu_BlacklistViewController *blackList = [[BanBu_BlacklistViewController alloc] init];
            [self.navigationController pushViewController:blackList animated:YES];
            [blackList release];
        }
        //个性化主题
        else if(indexPath.row ==2)
        {
            BanBu_ThemeAndBubbleViewController *theme = [[BanBu_ThemeAndBubbleViewController alloc] init];
            [self.navigationController pushViewController:theme animated:YES];
            [theme release];
        }
        //隐身模式设置
        else if(indexPath.row ==3)
        {
            BanBu_StealthViewController *steath = [[BanBu_StealthViewController alloc] init];
            [self.navigationController pushViewController:steath animated:YES];
            [steath release];
        }
    }
    else if(indexPath.section == 1)
    {
        UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        __block UITableViewCell * bcell = cell;
        //信息发送提示音
        if(indexPath.row == 3)
        {
            BanBu_SoundSetting *SendSound = [[BanBu_SoundSetting alloc]initWithStyle:UITableViewStyleGrouped];
            SendSound.isReceive = NO;//发消息提示音设置
            [SendSound setOpenOrCloseCallBack:^(BOOL isOn)
            {
                UILabel * label = (UILabel *)[bcell viewWithTag:1001];
                label.text = isOn?NSLocalizedString(@"open", nil):NSLocalizedString(@"close", nil);
                
            }];
            [self.navigationController pushViewController:SendSound animated:YES];
            [SendSound release];
        }
        //信息接收提示音
        else if(indexPath.row == 4)
        {
            BanBu_SoundSetting * receiveSound = [[BanBu_SoundSetting alloc]initWithStyle:UITableViewStyleGrouped];
            receiveSound.isReceive = YES;//接消息提示音设置
            [receiveSound setOpenOrCloseCallBack:^(BOOL isOn)
             {
                 UILabel * label = (UILabel *)[bcell viewWithTag:1001];
                 label.text = isOn?NSLocalizedString(@"open", nil):NSLocalizedString(@"close", nil);
                 
             }];
            [self.navigationController pushViewController:receiveSound animated:YES];
            [receiveSound release];
        }
        //推送勿扰模式
        else if (indexPath.row == 5)//勿扰设置
        {
            BanBu_NoDisturbingViewController * noDisturbing = [[BanBu_NoDisturbingViewController alloc]init];
            [noDisturbing setOpenOrCloseCallBack:^(BOOL isOn)
             {
                 UILabel * label = (UILabel *)[bcell viewWithTag:1001];
                 label.text = isOn?NSLocalizedString(@"open", nil):NSLocalizedString(@"close", nil);
                 
             }];
            [self.navigationController pushViewController:noDisturbing animated:YES];
            [noDisturbing release];
        }
    }
    else if(indexPath.section == 3)
    {
        //半圆测试平台
       if(!indexPath.row)
        {
            BanBu_WebFrame_ViewController * web = [[BanBu_WebFrame_ViewController alloc]init];
            [self.navigationController pushViewController:web animated:YES];
            [web release];
        }
        //清除缓存
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"clearCache", nil)
                                                            message:NSLocalizedString(@"clearCacheAlert", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"cancelNotice", nil)
                                                  otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
            alert.tag = 11;
            
            [alert show];
            [alert release];            
        }
        
    }
    else if (indexPath.section == 4)
    {
        //生成邀请码
        if(indexPath.row == 0)
        {
            [AppComManager getBanBuData:BanBu_Get_Invitecode_Byuser par:[NSDictionary dictionary] delegate:self];
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"newsState3", nil) activityAnimated:NO];
        }
        //通过短信邀请好友
        else
        {
            NSString* message = [NSString stringWithFormat:@"%@http://www.halfarea.com/beta/",NSLocalizedString(@"inviteFriend", nil)];
            NSString* number = @"";
            MFMessageComposeViewController* controller = [[MFMessageComposeViewController alloc] init];
            if ([MFMessageComposeViewController canSendText]) {
                controller.body =  message;
                //parse through receipients
                NSScanner* numberScanner = [NSScanner scannerWithString:number];
                [numberScanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@","]];
                NSCharacterSet* charactersToSkip = [NSCharacterSet characterSetWithCharactersInString:@","];
                NSString* substring = @"";
                NSMutableArray *substrings = [NSMutableArray array];
                while (![numberScanner isAtEnd]) {
                    [numberScanner scanUpToCharactersFromSet:charactersToSkip intoString:&substring];
                    [numberScanner scanCharactersFromSet:charactersToSkip intoString:NULL];
                    NSLog(@"%@", substring);
                    [substrings addObject:substring];
                }
                
                controller.recipients = substrings;
                controller.messageComposeDelegate = self;
                [self presentModalViewController:controller animated:YES];
            }
            

        }


    }
    else if (indexPath.section == 5)
    {
        //删除账户
        BanBu_DestoryAcountViewController *destory = [[BanBu_DestoryAcountViewController alloc] init];
        destory.settingDelegate = self;
        //        BanBu_NavigationController *nav = [[[BanBu_NavigationController alloc] initWithRootViewController:destory] autorelease];
        
        [self.navigationController pushViewController:destory animated:YES];
        [destory release];
    }
    else if (indexPath.section == 6&&indexPath.row == 0)
    {

    }
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    NSLog(@"010:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    [self dismissModalViewControllerAnimated:YES];
    if(result == MessageComposeResultCancelled)
    
    
    if (result == MessageComposeResultCancelled)
    {
         NSLog(@"Message cancelled");
    }
    else if (result == MessageComposeResultSent)
    {
        NSLog(@"Message sent");
    }
    else
    {
        NSLog(@"Message failed");
    }

}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"011:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    if(!buttonIndex)
    {
        BanBu_ChangePWViewController *changePW = [[BanBu_ChangePWViewController alloc] init];
        BanBu_NavigationController *nav = [[[BanBu_NavigationController alloc] initWithRootViewController:changePW] autorelease];
        if ([nav.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
        { 
            UIImage *image = [ThemeManager getThemeImage:@"topbar.png"];
            [nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
        }  

        [self presentViewController:nav animated:YES];
        [changePW release];
        
    }
    if(buttonIndex == 1) 
    {
        UIAlertView *alertView = [[UIAlertView alloc] 
                                  initWithTitle:NSLocalizedString(@"resetPSW", nil)
                                  message:NSLocalizedString(@"PSWinfo", nil)
                                  delegate:nil
                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                  otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
    }
}

-(void)switchAction:(UISwitch *)sender
{
    NSMutableDictionary *settingsDic = [NSMutableDictionary dictionaryWithDictionary:[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]];
    if(sender == _messageSwitch)
    {
    }
    else if(sender == _friendDosSwitch)
    {
        [MyAppDataManager.boolArr removeAllObjects];
        
        [MyAppDataManager.boolArr addObjectsFromArray: [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"boolKey"]];
        
        if(sender.on)
        {
            [MyAppDataManager.boolArr replaceObjectAtIndex:1 withObject:[NSNumber numberWithBool:1]];
        }
        else
        {
            [MyAppDataManager.boolArr replaceObjectAtIndex:1 withObject:[NSNumber numberWithBool:0]];
        }
        NSLog(@"接收好友圈:%@",MyAppDataManager.boolArr);
        [settingsDic setValue:MyAppDataManager.boolArr forKey:@"boolKey"];
    }
    else if(sender == _soundSwitch)//接消息音声音
    {
        if(sender.on)
        {
            MyAppDataManager.musicName = @"87888";
        }
        else
        {
            MyAppDataManager.musicName = @"";
        }
        
        [settingsDic setValue:MyAppDataManager.musicName forKey:@"MusicSwith"];        
    }
    else if (sender == _sendReadStateSwitch)//发送已读状态
    {
        [MyAppDataManager.boolArr removeAllObjects];
        
        [MyAppDataManager.boolArr addObject: [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"systemset_friendreadstatus"]];
        
        if (MyAppDataManager.boolArr.count<=0)
        {
            [MyAppDataManager.boolArr addObject:[NSNumber numberWithBool:1]];
        }
        
        if(sender.on)
        {
            [MyAppDataManager.boolArr replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:1]];
        }
        else
        {
            [MyAppDataManager.boolArr replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:0]];
        }
        [settingsDic setValue:[MyAppDataManager.boolArr objectAtIndex:0] forKey:@"systemset_friendreadstatus"];
    }
    else if (sender == _followYouNotificationSwitch)//我关注了你
    {
        NSNumber * number;
        
        if (sender.on)
        {
            number = [NSNumber numberWithBool:1];
        }
        else
        {
            number = [NSNumber numberWithBool:0];
        }
        [settingsDic setValue:number forKey:@"followYouNotification"];
    }
    else if (sender == _visitYouNotificationSwitch)//我查看了你的个人资料
    {
        NSNumber * number;
        
        if (sender.on)
        {
            number = [NSNumber numberWithBool:1];
        }
        else
        {
            number = [NSNumber numberWithBool:0];
        }
        [settingsDic setValue:number forKey:@"visitYouNotification"];
    }
    else if (sender == _takeCareNotificationSwitch)//新用户注册通知
    {
        NSNumber * number;
        
        if (sender.on)
        {
            number = [NSNumber numberWithBool:1];
        }
        else
        {
            number = [NSNumber numberWithBool:0];
        }
        [settingsDic setValue:number forKey:@"takeCareNotification"];
    }
    else if(sender == _setFollowsHiddenSwitch)//隐藏关注的人
    {
        NSNumber * number;
        NSString * sethidden;
        if (sender.on)
        {
            number = [NSNumber numberWithBool:1];
            sethidden = @"y";
        }
        else
        {
            number = [NSNumber numberWithBool:0];
            sethidden = @"n";
        }
//        [settingsDic setValue:number forKey:@"setFollowsHidden"];
        
        //        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //        dispatch_async(queue, ^{
        
        NSDictionary * parDic = [NSDictionary dictionaryWithObjectsAndKeys:sethidden,@"sethidden", nil];
        [AppComManager getBanBuData:BanBu_Set_MyFollowHidden par:parDic delegate:self];
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
        //提交服务器我要隐藏我关注的人，别人就看不到我关注的人了
        //        });
    }
    
    //更新数据
    NSMutableDictionary *settingsUpdata = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
    [settingsUpdata setValue:settingsDic forKey:@"settings"];
    
    [UserDefaults setValue:settingsUpdata forKey:MyAppDataManager.useruid];
    [UserDefaults synchronize];
    NSLog(@"记录2:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
    [self.tableView reloadData];
}

#pragma mark - 退出登录
- (void)exit:(UIButton *)button
{
    if([[UserDefaults valueForKey:@"isDefaultKey"] boolValue])
    {
        [self alertError:[NSString stringWithFormat:@"%@:%@%@%@",NSLocalizedString(@"currentPW", nil),[UserDefaults valueForKey:@"Pwd"],NSLocalizedString(@"changePW", nil),NSLocalizedString(@"mimaResetNotice", nil)]];
    }
    else
    {
        [self alertView:nil clickedButtonAtIndex:1];
    }

}

- (void)alertError:(NSString *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"mimaRestTitle", nil) message:error delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"exitBtn", nil),NSLocalizedString(@"changePWTitle",nil), nil];
    alert.tag = 10;
    [alert show];
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    anim.repeatCount = 1;
    anim.values = @[@-10, @10, @-10, @10, @-10];
    [self.view.layer addAnimation:anim forKey:nil];
}


#pragma mark - BanBuDelegate

- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error{
    
    self.navigationController.view.userInteractionEnabled = YES;
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:YES afterShow:0.0];
    if(error)
    {
        return;
    }
    if([AppComManager respondsDic:resDic isFunctionData:BanBu_Set_MyFollowHidden])
    {
        NSLog(@"BanBu_Set_MyFollowHidden:%@",resDic);
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            _setFollowsHiddenSwitch.on = !_setFollowsHiddenSwitch.on;
            NSMutableDictionary *settingsDic = [NSMutableDictionary dictionaryWithDictionary:[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]];
             NSNumber * number;
            if ([[settingsDic valueForKey:@"setFollowsHidden"] boolValue])
            {
                number = [NSNumber numberWithBool:0];

                
            }
            else
            {
                number = [NSNumber numberWithBool:1];
            }
            [settingsDic setValue:number forKey:@"setFollowsHidden"];
            NSMutableDictionary *settingsUpdata = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
            [settingsUpdata setValue:settingsDic forKey:@"settings"];
            [UserDefaults setValue:settingsUpdata forKey:MyAppDataManager.useruid];
            [UserDefaults synchronize];
            NSLog(@"记录222:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);


        }
        else
        {
//            _setFollowsHiddenSwitch.on = !_setFollowsHiddenSwitch.on;
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:[MyAppDataManager IsInternationalLanguage:[resDic valueForKey:@"error"]] activityAnimated:NO duration:1];
        }
        
    }
    if([AppComManager respondsDic:resDic isFunctionData:Banbu_Set_User_Pushid])
    {
        if([[resDic valueForKey:@"ok"]boolValue])
        {
            [self.navigationController popViewControllerAnimated:YES];
            
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
            BanBu_ListViewController *acontrol = (BanBu_ListViewController *)[self.tabBarController.viewControllers objectAtIndex:0];
            acontrol.currentPage = 0;
            BanBu_BroadcastTVC *bcontrol = (BanBu_BroadcastTVC *)[self.tabBarController.viewControllers objectAtIndex:0];
            bcontrol.DosPage = 0;
            
            
            NSArray *guanxi = [NSArray arrayWithObjects:@"g",@"f",@"h", nil];
            
            for (int i=0; i<guanxi.count; i++)
            {
                NSString *path = [DataCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@listdata",MyAppDataManager.useruid,[guanxi objectAtIndex:i]]];
                [FileManager removeItemAtPath:path error:nil];
            }

            [UserDefaults synchronize];
            
            [MyAppDataManager.friendsDos removeAllObjects];
            [MyAppDataManager.contentArr removeAllObjects];
            [MyAppDataManager.friendViewList removeAllObjects];
            [MyAppDataManager.friends removeAllObjects];
            
            NSLog(@"012:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
            NSMutableDictionary *uidDic = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
            [uidDic removeObjectForKey:@"loginid"];
            [UserDefaults setValue:uidDic forKey:MyAppDataManager.useruid];
            [UserDefaults synchronize];
            NSLog(@"013:%@",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]);
            BanBu_AppDelegate *delegate = (BanBu_AppDelegate *)[UIApplication sharedApplication].delegate;
            [delegate setViewController:NO];
            
            [AppComManager.networkQueue cancelAllOperations];
        }
        else
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
    }
    else if ([AppComManager respondsDic:resDic isFunctionData:BanBu_Get_Invitecode_Byuser])
    {
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"inviteSucceed", nil) activityAnimated:NO duration:2];
        }
        else
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"inviteFail", nil) activityAnimated:NO duration:2];
        }
    }
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //清理缓存
    if(alertView.tag == 11)
    {
        switch (buttonIndex)
        {
            case 0:
                break;
            case 1:
            {
                //清除记录动态时间的缓存2014/07/10
                [BanBu_CommUtils clearLocalData:DynamicKey];
                
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
                NSString *cachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"];
                [FileManager removeItemAtPath:cachePath error:nil];
                [[SDImageCache sharedImageCache] clearDisk];
                [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES duration:2];
            }
                break;
            default:
                break;
        }
    }
    else
    {
        {
            //退出登录
            if (buttonIndex == 1)//退出
            {
                //切换账号，删除之前的收藏缓存
                [BanBu_CommUtils setLocalData:nil key:CollectDataKey];
                
                if([AppComManager.receiveMsgTimer isValid])
                {
                    [AppComManager.receiveMsgTimer invalidate];
                    
                    AppComManager.receiveMsgTimer=nil;
                }
                [AppComManager stopReceiveMsgForUid:nil];
                [AppComManager.receiveMsgQueue cancelAllOperations];
                
                NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[UserDefaults valueForKey:[UserDefaults valueForKey:@"myID"]]];
                [dic removeObjectForKey:@"loginid"];
                [MyAppDataManager.talkPeoples removeAllObjects];
                [MyAppDataManager.secondaryTalkPeople removeAllObjects];
                [MyAppDataManager.dialogs removeAllObjects];
                
                MyAppDataManager.useruid = @"";
                [UserDefaults setObject:dic forKey:[UserDefaults valueForKey:@"myID"]];
                [UserDefaults synchronize];
                //    [UserDefaults removeObjectForKey:@"loginid"];
                [dic release];
                NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
                [parDic setValue:@"apple" forKey:@"server"];
                [parDic setValue:@"" forKey:@"pushid"];
                [AppComManager getBanBuData:Banbu_Set_User_Pushid par:parDic delegate:self];
                
                [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
                AppComManager.isReceivingDialods = NO;
                
            }
            else if(buttonIndex == 2)//更改密码
            {
                BanBu_ChangePWViewController * cvc = [[BanBu_ChangePWViewController alloc]init];
                [self.navigationController pushViewController:cvc animated:YES];
            }
            
        }
    }
}






@end
